﻿Public Class frmPROrder
    Dim Strsql As String
    Dim Mysql As New ClsSQLhelper
    Public LockStatus As Boolean = False
    Public MyOrdernumber As Integer
    Dim On_load As Boolean = True
    Dim MyWhis As New cls_whisframework
    Sub Load_CboSite()
        On_load = True
        Dim DS_Site As New DataSet
        Strsql = "Select siteid,site from site order by site"
        Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
        DS_Site = Mysql.GetMYSQLDataset(Strsql, "DSSite", "Site")
        With Cbo_site
            .DataSource = DS_Site.Tables(0)
            .DisplayMember = "site"
            .ValueMember = "siteid"
            .SelectionStart = 0
            If My.Settings.CurrentSite <> "" Then
                .SelectedValue = My.Settings.CurrentSite
            End If
        End With
        DS_Site.Dispose()
        On_load = False
    End Sub
   
    Sub Load_Data(Optional ByVal PrOrder As String = "", Optional ByVal ShowComplete As Boolean = False, Optional ByVal ShowCancel As Boolean = False, Optional ByVal ShowNotApproved As Boolean = False)

        Try
            Strsql = "select orderdate,ordernumber,site.site,orderedby,departments.depname as depname,reason,orderstatus.orderstatus,yearbudget from prorder "
            Strsql = Strsql & " left join departments on prorder.department = departments.depcode "
            Strsql = Strsql & " left join site on prorder.site= site.siteid "
            Strsql = Strsql & " left join orderstatus on prorder.orderstatus= orderstatus.orderstatusid"
            Strsql = Strsql & " where orderstatus.statustype = 'pr'  and prorder.site = " & My.Settings.CurrentSite & " and prorder.orderstatus in  (0 "
            If ShowComplete = True Then Strsql = Strsql & ",1"
            If ShowCancel = True Then Strsql = Strsql & " ,2"
            If ShowNotApproved = True Then Strsql = Strsql & ",3"
            Strsql = Strsql & ")"
            Dim DTProrder As New DataTable
            DTProrder = Mysql.GetMYSQLDataTable(Strsql, "prorder")

            With Me.GridOrder
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .ReadOnly = True
                .DataSource = DTProrder

                .Columns("orderdate").HeaderText = "วันที่ขอ"
                .Columns("orderdate").DataPropertyName = "orderdate"
                .Columns("ordernumber").HeaderText = "เลขที่"
                .Columns("ordernumber").DataPropertyName = "ordernumber"
                .Columns("site").HeaderText = "คลังวัสดุ"
                .Columns("site").Width = 200
                .Columns("site").DataPropertyName = "site"
                .Columns("orderedby").HeaderText = "โดย"
                .Columns("depname").HeaderText = "แผนก"
                .Columns("depname").Width = 200
                .Columns("depname").DataPropertyName = "depname"
                .Columns("reason").HeaderText = "เหตุผล"
                .Columns("reason").DataPropertyName = "reason"
                .Columns("yearbudget").HeaderText = "ปีงบประมาณ"
                .Columns("yearbudget").DataPropertyName = "yearbudget"
                .Columns("orderstatus").HeaderText = "สถาณะ"
                .Columns("orderstatus").DataPropertyName = "orderstatus"
            End With


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub frmPROrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            EventLog1.Source = "WHIS"
            EventLog1.Log = "WHIS"
            Load_CboSite()

            Load_Data()


            If My.Settings.CurrentuserGroup <> 1 Then Me.Cbo_site.Enabled = False
        Catch ex As Exception

        End Try

    End Sub

    Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
        frmPRDetail.NewEntry_State = True
        frmPRDetail.MdiParent = frmmain
        frmPRDetail.Show()
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub
    Private Sub GridOrder_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrder.CellDoubleClick

        frmPRDetail.TxtOrderNumber.ReadOnly = True
        frmPRDetail.NewEntry_State = False
        frmPRDetail.TxtOrderNumber.Text = Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(1).Value
        frmPRDetail.MdiParent = frmmain
        frmPRDetail.Show()
    End Sub



    Private Sub DTPStart_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPStart.ValueChanged
        Load_Data()
    End Sub

    Private Sub DTPEnd_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPEnd.ValueChanged
        Load_Data()
    End Sub

    Private Sub Cbo_site_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbo_site.SelectedIndexChanged

        If On_load = False Then Load_Data()
    End Sub

 
    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmmain.mSpk.Play("ReadContinued")
        Load_Data(Me.TxtSearch.Text)
    End Sub

    Private Sub TxtPO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.KeyCode = Keys.Enter Then
                Load_Data(Me.TxtSearch.Text)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        frmPRDetail.TxtOrderNumber.ReadOnly = True
        frmPRDetail.NewEntry_State = False
        frmPRDetail.TxtOrderNumber.Text = Me.GridOrder.Rows(GridOrder.CurrentCell.RowIndex).Cells(1).Value
        frmPRDetail.MdiParent = frmmain
        frmPRDetail.Show()
    End Sub

    Private Sub GridOrder_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridOrder.CellContentClick

    End Sub

    Private Sub Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint

    End Sub

    Private Sub chkShowComplete_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowComplete.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub

    Private Sub chkShowCancel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowCancel.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub

    Private Sub chkShowNotapproved_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkShowNotapproved.CheckedChanged
        Load_Data("", chkShowComplete.Checked, chkShowCancel.Checked, chkShowNotapproved.Checked)
    End Sub
End Class